﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DOTNET101_String
{
    class Program
    {
        static void Main(string[] args)
        {

            string s1 = "orange";
            string s2 = "red";

            s1 += s2;
            System.Console.WriteLine(s1);  // outputs "orangered"

            s1 = s1.Substring(2, 5);
            System.Console.WriteLine(s1);  // outputs "anger"

            s1 = "Hello";
            s2 = s1;
            s1 += " and goodbye.";
            Console.WriteLine(s2); //outputs "Hello"

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("one ");
            sb.Append("two ");
            sb.Append("three");
            string str = sb.ToString();
            System.Console.WriteLine(str);

            // Outputs: one two three

            string p1 = "\\\\My Documents\\My Files\\";
            string p2 = @"\\My Documents\My Files\";

            string s = @"You say ""goodbye"" and I say ""hello""";

            string s3 = "Visual C# Express";

            System.Console.WriteLine(s3.Substring(7, 2));         // outputs "C#"
            System.Console.WriteLine(s3.Replace("C#", "Basic"));  // outputs "Visual Basic Express"

            string s4 = "Hello, World";
            char[] arr = s4.ToCharArray(0, s4.Length);

            foreach (char c in arr)
            {
                System.Console.Write(c);  // outputs "Hello, World"
            }

            string s5 = "Printing backwards";

            for (int i = 0; i < s5.Length; i++)
            {
                System.Console.Write(s5[s5.Length - i - 1]);  // outputs "sdrawkcab gnitnirP"
            }

            string s6 = "Battle of Hastings, 1066";

            System.Console.WriteLine(s6.ToUpper());  // outputs "BATTLE OF HASTINGS 1066"
            System.Console.WriteLine(s6.ToLower());  // outputs "battle of hastings 1066"

            string color1 = "red";
            string color2 = "green";
            string color3 = "red";

            if (color1 == color3)
            {
                System.Console.WriteLine("Equal");
            }
            if (color1 != color2)
            {
                System.Console.WriteLine("Not equal");
            }

            // Enter different values for string1 and string2 to
            // experiement with behavior of CompareTo
            string string1 = "ABC";
            string string2 = "abc";

            int result = string1.CompareTo(string2);

            if (result > 0)
            {
                System.Console.WriteLine("{0} is greater than {1}", string1, string2);
            }
            else if (result == 0)
            {
                System.Console.WriteLine("{0} is equal to {1}", string1, string2);
            }
            else if (result < 0)
            {
                System.Console.WriteLine("{0} is less than {1}", string1, string2);
            }
            // Outputs: ABC is less than abc

            string s9 = "Battle of Hastings, 1066";

            System.Console.WriteLine(s9.IndexOf("Hastings"));  // outputs 10
            System.Console.WriteLine(s9.IndexOf("1967"));      // outputs -1

            char[] delimit = new char[] { ' ' };
            string s10 = "The cat sat on the mat.";
            foreach (string substr in s10.Split(delimit))
            {
                System.Console.WriteLine(substr);
            }

            str = "hello";
            string nullStr = null;
            string emptyStr = "";

            string tempStr = str + nullStr; // tempStr = "hello"
            bool b = (emptyStr == nullStr);// b = false;
            // emptyStr + nullStr = ""; // creates a new empty string
            // int len = nullStr.Length; // throws NullReferenceException

            System.Text.StringBuilder sbuilder = new System.Text.StringBuilder("Rat: the ideal pet");
            sbuilder[0] = 'C';
            System.Console.WriteLine(sbuilder.ToString());
            System.Console.ReadLine();

            //Outputs Cat: the ideal pet

            TestStringBuilder.TestBuilder();
            TestStringSplit.TestSplit();
            TestStringSearch.TestSearch();
            TestRegularExpressions.TestReqular();   
        }
    }

    class TestStringBuilder
    {
        public static void TestBuilder()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            // Create a string composed of numbers 0 - 9
            for (int i = 0; i < 10; i++)
            {
                sb.Append(i.ToString());
            }
            System.Console.WriteLine(sb);  // displays 0123456789

            // Copy one character of the string (not possible with a System.String)
            sb[0] = sb[9];

            System.Console.WriteLine(sb);  // displays 9123456789
        }
    }

    class TestStringSplit
    {
        public static void TestSplit()
        {
            char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

            string text = "one\ttwo three:four,five six seven";
            System.Console.WriteLine("Original text: '{0}'", text);

            string[] words = text.Split(delimiterChars);
            System.Console.WriteLine("{0} words in text:", words.Length);

            foreach (string s in words)
            {
                System.Console.WriteLine(s);
            }
        }
    }

    class TestStringSearch
    {
        public static void TestSearch()
        {
            string str = "Extension methods have all the capabilities of regular static methods.";
            // Write the string and include the quotation marks
            System.Console.WriteLine("\"{0}\"", str);

            bool test1 = str.StartsWith("extension");
            System.Console.WriteLine("starts with \"extension\"? {0}", test1);

            bool test2 = str.StartsWith("extension", System.StringComparison.OrdinalIgnoreCase);
            System.Console.WriteLine("starts with \"extension\"? {0} (ignoring case)", test2);

            bool test3 = str.EndsWith(".");
            System.Console.WriteLine("ends with '.'? {0}", test3);

            int first = str.IndexOf("method");
            int last = str.LastIndexOf("method");
            string str2 = str.Substring(first, last - first);
            System.Console.WriteLine("between two \"method\" words: '{0}'", str2);

            // Keep the console window open in debug mode
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }

    class TestRegularExpressions
    {
        public static void TestReqular()
        {
            string[] sentences = 
        {
            "cow over the moon",
            "Betsy the Cow",
            "cowering in the corner",
            "no match here"
        };

            string sPattern = "cow";

            foreach (string s in sentences)
            {
                System.Console.Write("{0,24}", s);

                if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                {
                    System.Console.WriteLine("  (match for '{0}' found)", sPattern);
                }
                else
                {
                    System.Console.WriteLine();
                }
            }
        }
    }

    class TestRegularExpressionValidation
    {
        public static void TestValidation()
        {
            string[] numbers = 
        {
            "123-456-7890", 
            "444-234-22450", 
            "690-203-6578", 
            "146-893-232",
            "146-839-2322",
            "4007-295-1111", 
            "407-295-1111", 
            "407-2-5555", 
        };

            string sPattern = "^\\d{3}-\\d{3}-\\d{4}$";

            foreach (string s in numbers)
            {
                System.Console.Write("{0,14}", s);

                if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern))
                {
                    System.Console.WriteLine(" - valid");
                }
                else
                {
                    System.Console.WriteLine(" - invalid");
                }
            }
        }
    }

    class TestModifyStrings
    {
        public static void TestModify()
        {
            string str = "The quick brown fox jumped over the fence";
            System.Console.WriteLine(str);

            char[] chars = str.ToCharArray();
            int animalIndex = str.IndexOf("fox");
            if (animalIndex != -1)
            {
                chars[animalIndex++] = 'c';
                chars[animalIndex++] = 'a';
                chars[animalIndex] = 't';
            }

            string str2 = new string(chars);
            System.Console.WriteLine(str2);
        }

        public static void TestNumeric()
        {
            string s = "1287543"; //"1287543.0" will return false for a long
            long number1 = 0;
            bool result = long.TryParse(s, out number1);
            if (result == true)
                Console.WriteLine("number1 now = {0}", number1);
            else
                Console.WriteLine("s is not a valid long");

            byte number2 = 0;
            s = "255"; // A value of 256 will return false
            result = byte.TryParse(s, out number2);
            if (result == true)
                Console.WriteLine("number2 now = {0}", number2);
            else
                Console.WriteLine("s is not a valid byte");

            decimal number3 = 0;
            s = "27.3"; //"27" is also a valid decimal
            result = decimal.TryParse(s, out number3);
            if (result == true)
                Console.WriteLine("number3 now = {0}", number3);
            else
                Console.WriteLine("number3 is not a valid decimal");
        }
    }
}
